package com.baigt.test;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

public class LambdaInJvm {
    public static void main(String[] args2) throws ClassNotFoundException, InterruptedException {
        System.setProperty("jdk.internal.lambda.dumpProxyClasses", "d:\\data\\");
        System.out.println("成功");
//        SelfClassLoader loader=new SelfClassLoader();
//        loader.findClass("ricky");
        Enhancer enhancer=new Enhancer();
        enhancer.setSuperclass(Runnable.class);
        enhancer.setUseCache(false);
        enhancer.setCallback((MethodInterceptor)(Object obj, java.lang.reflect.Method method, Object[] args,
                                                 MethodProxy proxy)->proxy.invokeSuper(obj,args));
        Object o = enhancer.create();
        System.out.println(o.toString());
        System.out.println("hello");
        TimeUnit.MINUTES.sleep(100);

    }
}

class SelfClassLoader extends ClassLoader {
    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        String content="package com.baigt.test; class "+name+" { public "+name+"(){} static void test(){ Thread thread=new Thread(()->{});  } }";
        byte[] data=null;
        ByteArrayOutputStream bos=new ByteArrayOutputStream();

        bos.write(-54);
        bos.write(-2);
        bos.write(-70);
        bos.write(-66);
        bos.write(0);
        bos.write(0);
        bos.write(0);
        bos.write(52);
//        bos.write(0);
//        bos.write(0);
        try {
            bos.write(content.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
        data=bos.toByteArray();
        return this.defineClass(name, data, 0, data.length);
    }
}
class ricky1{ public ricky1(){} static void test(){ Thread thread=new Thread(()->{});  } }
